package com.stenio.fancat.controller.shop;

import com.baomidou.mybatisplus.plugins.Page;
import com.stenio.fancat.controller.Result;
import com.stenio.fancat.domain.shop.FcVender;
import com.stenio.fancat.form.shop.VenderForm;
import com.stenio.fancat.service.shop.VenderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangqiu
 * @date 2018/12/1
 * @desc
 **/
@RestController
@RequestMapping("vender")
public class VenderController {
    @Autowired
    private VenderService venderService;

    /**
     * 查商家信息
     * @param id
     * @return
     */
    @GetMapping("get/{id}")
    @ResponseBody
    public Result<FcVender> get(@PathVariable Integer id){
        Result<FcVender> result = new Result<>();
        result.setData(venderService.findById(id));
        return result;
    }


    @GetMapping("getAll")
    @ResponseBody
    public Result<List<FcVender>> getAll(){
        Result<List<FcVender>> result = new Result<>();
        result.setData(venderService.findAll());
        return result;
    }

    @PostMapping("page")
    @ResponseBody
    public Result page(@RequestBody VenderForm venderForm){
        Result<Map<String,Object>> result = new Result();
        Page<FcVender> page = venderService.selectByPage(venderForm);
        Map<String,Object> data = new HashMap<>();
        if(page!=null){
            data.put("current",page.getCurrent());
            data.put("pages",page.getPages());
            data.put("size",page.getSize());
            data.put("total",page.getTotal());
            data.put("records",page.getRecords());
        }
        result.setData(data);
        return result;
    }

    @PostMapping("add")
    @ResponseBody
    public Result<FcVender> add(@RequestBody VenderForm venderForm){
        Result<FcVender> result = new Result();
        FcVender vender = venderService.saveWare(venderForm);
        result.setData(vender);
        return result;
    }

    @PostMapping("update")
    @ResponseBody
    public Result update(@RequestBody VenderForm venderForm){
        Result<FcVender> result = new Result();
        result.setData(venderService.updateVender(venderForm));
        return result;
    }
}
